package com.chanyachting.emall.action.applicationLeter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Applicationleter;
import com.chanyachting.emall.form.ApplicationleterForm;
import com.chanyachting.emall.service.ApplicationLeterService;
import com.chanyachting.emall.serviceImpl.ApplicationLeterServiceImpl;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.MyFile;
import com.chanyachting.emall.util.RequestUtil;

public class UpdateAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ApplicationleterForm alForm=(ApplicationleterForm)form;
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		if(request.getMethod().equals("POST")){
			
			ApplicationLeterService alservice=new ApplicationLeterServiceImpl();
			
			Applicationleter al=new Applicationleter();
			
			BeanUtils.copyProperties(al, alForm);
			
			String imgUrl=MyFile.saveFileForm(alForm.getFile(), request);
			
			if(imgUrl!=null && !imgUrl.equals(""))al.setMemberPicUrl(imgUrl);  //设置图片访问地址		
			
			al.setId(id);
			
			al.setUpdateDate(DateUtil.getDate());
			
			String selectYear=ru.getParameter("selectYear", "1930");
			String selectMonth=ru.getParameter("selectMonth", "1");
			
			al.setInJobDate(selectYear+"-"+selectMonth);  //设置到职日期
	
			boolean temp=alservice.updateApplicationleterInfo(al);

			if(temp){
				
				ru.setSuccessMsg();
				
			}else{
				
				ru.setFailMsg();
			}
			
		}
		
		//从新加载
		Applicationleter al=new ApplicationLeterServiceImpl().getOneApplicationLeter(new Applicationleter(id));
		
		ru.setAttribute("Applicationleter", al);
		
		return mapping.findForward("update");
	}
	
	
	

}
